package com.pricl.frame.common.web.rest;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springside.modules.web.MediaTypes;

/**
 * <ol>
 * <li>前端APP异常处理类</li>
 * <ol>
 * 
 * @author jackzhong
 * @since 2014年10月25日下午11:51:32
 */
public class AppExceptionResolver implements HandlerExceptionResolver {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.web.servlet.HandlerExceptionResolver#resolveException
	 * (javax.servlet.http.HttpServletRequest,
	 * javax.servlet.http.HttpServletResponse, java.lang.Object,
	 * java.lang.Exception)
	 */
	@Override
	public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
		String jsonString = new RestResponseBuilder(RestResponseCode.getFailureResponseCode(), "系统内部维护，请稍候!").builderToJsonString();
		response.setContentType(MediaTypes.JSON_UTF_8);
		response.setStatus(HttpServletResponse.SC_OK);
		PrintWriter writer = null;
		try {
			writer = response.getWriter();
			writer.write(jsonString);
			writer.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (writer != null) {
				writer.close();
			}
		}
		return null;
	}
}
